package util.yahoo;

import java.io.StringReader;

import org.apache.commons.digester.Digester;

import vo.yahoo.Folder;
import vo.yahoo.Folders;

public class FolderDigester {

	public static void main(String[] args) {
		String str = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><ListFoldersResponse><numberOfFolders>5</numberOfFolders><folder unread=\"0\" total=\"0\" size=\"0\" isSystem=\"true\"><folderInfo fid=\"%40B%40Bulk\" name=\"@B@Bulk\"/></folder><folder unread=\"0\" total=\"0\" size=\"0\" isSystem=\"true\"><folderInfo fid=\"Draft\" name=\"Draft\"/></folder><folder unread=\"0\" total=\"3\" size=\"17457\" isSystem=\"true\"><folderInfo fid=\"Inbox\" name=\"Inbox\"/></folder><folder unread=\"0\" total=\"0\" size=\"0\" isSystem=\"true\"><folderInfo fid=\"Sent\" name=\"Sent\"/></folder><folder unread=\"0\" total=\"0\" size=\"0\" isSystem=\"true\"><folderInfo fid=\"Trash\" name=\"Trash\"/></folder></ListFoldersResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>";
		Folders fldr = (Folders) parse(str);
	}
	
	public static Object parse(String folderSoapResponse) {
		Folders folders = null;
		try {
			Digester digester = new Digester();
			digester.setValidating(false);

			digester.addObjectCreate("SOAP-ENV:Envelope/SOAP-ENV:Body/ListFoldersResponse",
				Folders.class);
			digester.addObjectCreate(
				"SOAP-ENV:Envelope/SOAP-ENV:Body/ListFoldersResponse/folder",
				Folder.class);
			digester.addSetProperties("SOAP-ENV:Envelope/SOAP-ENV:Body/ListFoldersResponse/folder",
				"unread", "unread");
			digester
					.addSetProperties(
							"SOAP-ENV:Envelope/SOAP-ENV:Body/ListFoldersResponse/folder",
							"total", "total");
			digester
					.addSetProperties(
							"SOAP-ENV:Envelope/SOAP-ENV:Body/ListFoldersResponse/folder",
							"size", "size");
			digester
					.addSetProperties(
							"SOAP-ENV:Envelope/SOAP-ENV:Body/ListFoldersResponse/folder",
							"isSystem", "isSystem");
			digester
					.addSetProperties(
							"SOAP-ENV:Envelope/SOAP-ENV:Body/ListFoldersResponse/folder/folderInfo",
							"name", "name");
			digester.addSetNext( "SOAP-ENV:Envelope/SOAP-ENV:Body/ListFoldersResponse/folder", "addFolder" );
			
			StringReader sr = new StringReader(folderSoapResponse);
			folders = (Folders) digester.parse(sr);

		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return folders;
	}
}